package android.FiltrosJava;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;

public class Rotate90 extends AbstractImageBasic{
	private int orientacao = 0;
	
	public void set90CW(){
		orientacao = 0;
	}
	
	public void set90CCW(){
		orientacao = 1;
	}
	
	public void set180(){
		orientacao = 2;
	}
	
	public BufferedImage applyFilter(BufferedImage src) {		
		switch (orientacao){
			case 0: src = Gira90CW(src);
					break;
			case 1: src = Gira90CCW(src);
					break;
			case 2: src = Gira180(src);
					break;
		}
		
		return src;
	}
	
	private BufferedImage Gira90CW(BufferedImage src){
		int width = src.getWidth();
        int height = src.getHeight();
        
        //*
        ColorModel dstCM = src.getColorModel();
		BufferedImage dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(height, width), dstCM.isAlphaPremultiplied(), null);
        //*/        
        //BufferedImage dst = new BufferedImage(src.getHeight(null), src.getWidth(null), BufferedImage.TYPE_3BYTE_BGR);
        
    	WritableRaster raster_src = src.getRaster();    	    	
    	WritableRaster raster_dst = dst.getRaster(); 
    	    	    	
    	int numBands = raster_src.getNumBands();
    	int[] imageData = new int[numBands];  
    	    	    	
    	for ( int y = 0; y < height; y++ ) {
    		for ( int x = 0; x < width; x++ ) {
    			raster_src.getPixel(x,y,imageData);    			
    			raster_dst.setPixel(height - y - 1,x,imageData);    			  			
    		}
    	}
		
		return dst;
	}
	
	private BufferedImage Gira90CCW(BufferedImage src){
		int width = src.getWidth();
        int height = src.getHeight();
        
        ColorModel dstCM = src.getColorModel();
		BufferedImage dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(height, width), dstCM.isAlphaPremultiplied(), null);
                
    	WritableRaster raster_src = src.getRaster();    	    	
    	WritableRaster raster_dst = dst.getRaster(); 
    	    	    	
    	int numBands = raster_src.getNumBands();
    	int[] imageData = new int[numBands];  
    	    	    	
    	for ( int y = 0; y < height; y++ ) {
    		for ( int x = 0; x < width; x++ ) {
    			raster_src.getPixel(x,y,imageData);    			
    			raster_dst.setPixel(y,width - x -1,imageData);    			    			
    		}
    	}
						
		return dst;
	}
	
	private BufferedImage Gira180(BufferedImage src){			
		int width = src.getWidth();
        int height = src.getHeight();
        int px, py;     
        
    	WritableRaster raster_src = src.getRaster();    	    	
    	    	    	    	
    	int numBands = raster_src.getNumBands();
    	int[] imageData = new int[numBands];  
    	int[] imageData1 = new int[numBands];    	
    	    	    	
    	for ( int y = 0; y < height/2; y++ ) {
    		for ( int x = 0; x < width; x++ ) {    			
    			
    			px = width - x - 1;
    			py = height - y - 1;
    			raster_src.getPixel(x,y,imageData);    			
    			raster_src.getPixel(px,py,imageData1);
    			
    			raster_src.setPixel(x,y,imageData1);    			    			
    			raster_src.setPixel(px,py,imageData);
    		}
    	}
    	
    	if ((height % 2) == 1){    		
    		int meio = (int) Math.ceil(height/2.0) - 1;
    		for ( int x = 0; x < width/2; x++ ) {
    			px = width - x - 1;    			
    			raster_src.getPixel(x,meio,imageData);    			
    			raster_src.getPixel(px,meio,imageData1);
    			
    			raster_src.setPixel(x,meio,imageData1);    			    			
    			raster_src.setPixel(px,meio,imageData);
    		}    		
    	}
						
		return src;
}
}
